<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="../../../util/session.jsp" flush="true" />
<%@page import="connection.ConnectionOracleOR, java.sql.*,cadastro.Usuario, connection.QueryOracleOR, java.util.*"%>
<%

String action = request.getParameter("action");

int NID_USUARIO=Integer.parseInt( request.getParameter("NID_USUARIO") );
String VNOME="";
int NMATRICULA=0;
String VCARGO="";
String VEMAIL="";
String VLOGIN="";
String VSENHA="";
String CTIPO=""; 
int NID_DEPARTAMENTO=Integer.parseInt( request.getParameter("NID_DEPARTAMENTO") );
String VNOME_DPTO=""; 
int NID_EMPRESA=0;
String VRAZAO_SOCIAL="";


	if (request.getMethod().equalsIgnoreCase("post")) {

		String task = request.getParameter("task");
		
		
		
		if( task.equals( "save" ) ){						
			
			//NID_EMPRESA			=	Integer.parseInt( request.getParameter("NID_EMPRESA") );
			//NID_DEPARTAMENTO	=	Integer.parseInt( request.getParameter("NID_DEPARTAMENTO") );
			//System.out.println("NID_EMPRESA="+NID_EMPRESA);


			try{
				
				Usuario usuario = new Usuario();

				usuario.save( 						
							NID_USUARIO,
							request.getParameter("VNOME"),
							Integer.parseInt( request.getParameter("NMATRICULA") ),
							request.getParameter("VCARGO"),
							request.getParameter("VEMAIL"),
							request.getParameter("VLOGIN"),
							request.getParameter("VSENHA"),
							request.getParameter("CTIPO"),
							Integer.parseInt(request.getParameter("NID_DEPARTAMENTO") ),
							Integer.parseInt( request.getParameter("NID_EMPRESA") ),
							request.getParameter("CTIPO_ATUAL") );
									
			}catch( Exception e){
				
				out.print( e.getMessage() );
			}
			
				
			if( NID_USUARIO == 0){
				
				//response.sendRedirect("../");
				out.print("<script>window.location.href='/repositoriovideo/cadastro/usuario/?NID_USUARIO="+NID_USUARIO+"&NID_EMPRESA="+NID_EMPRESA+"&NID_DEPARTAMENTO="+NID_DEPARTAMENTO+"';</script>");
				
			}else{
				//response.sendRedirect("?action=editar&NID_EMPRESA="+NID_EMPRESA);
				out.print("<script>window.location.href='/repositoriovideo/cadastro/usuario/editar/?action=editar&NID_USUARIO="+NID_USUARIO+"&NID_EMPRESA="+NID_EMPRESA+"&NID_DEPARTAMENTO="+NID_DEPARTAMENTO+"';</script>");
				
			}
				
			
				
				
			
		}else if( task.equals( "remover" ) ){
			//tem que implementar
			//copy/past
			
		}else{
			
			//request.setAttribute("mensagem", "Ação inválida");
			//request.getRequestDispatcher("../").forward(request, response);
			
		}
		
		
	}else{
		
		
		//NID_EMPRESA = Integer.parseInt( request.getParameter("NID_EMPRESA") );
		
		
		if( action.equals( "editar" ) ){
						
			
			if( NID_USUARIO == 0 ){
				//novo
				
			}else{
				//editar
				
				
				try{
					
					Usuario usuario = new Usuario();

					ResultSet rs =	usuario.load( NID_USUARIO );
					
					while (rs.next()){
						
						NID_USUARIO=rs.getInt("NID_USUARIO");
						VNOME=rs.getString("VNOME");
						NMATRICULA=rs.getInt("NMATRICULA");
						VCARGO=rs.getString("VCARGO");
						VEMAIL=rs.getString("VEMAIL");
						VLOGIN=rs.getString("VLOGIN");
						VSENHA=rs.getString("VSENHA");
						CTIPO=rs.getString("CTIPO");						
						NID_DEPARTAMENTO=rs.getInt("NID_DEPARTAMENTO");
						VNOME_DPTO=rs.getString("VNOME_DPTO");
						NID_EMPRESA=rs.getInt("NID_EMPRESA");
						VRAZAO_SOCIAL=rs.getString("VRAZAO_SOCIAL");
						
			        	
			        }
					
				}catch( Exception e){
					
					out.print( e.getMessage() );
				}
				
			}
			
			
			
		}else if( action.equals( "remover" ) ){
			
			if( NID_USUARIO ==0 ){
				//nao faz nada, tem que lancar um alerta
				
			}else{
				//exclui
				
				try{
					
					Usuario usuario = new Usuario();

					usuario.remover( NID_USUARIO );
					
					//response.sendRedirect("/repositoriovideo/cadastro/empresa/");
					//out.print("<script>window.location.href='/repositoriovideo/cadastro/departamento/?NID_EMRPESA="+NID_EMPRESA+"';</script>");
					out.print("<script>window.location.href='/repositoriovideo/cadastro/usuario/';</script>");
					
					//System.exit(0);
					
				}catch( Exception e){
					
					out.print( e.getMessage() );
				}
				
			}
			
		}else{
			
			//request.setAttribute("mensagem", "Ação inválida");
			//request.getRequestDispatcher("../").forward(request, response);
			
		}
		
	}

	


%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Repositório de Vídeo</title>

<link href="../../../css/style.css" type="text/css" rel="stylesheet" />

<script>

function submitbutton(pressbutton) 
{
	var form = document.adminForm;

	switch( pressbutton )
	{
		
		case	"save":
		{
			if (confirm ("Deseja salvar?")) {
				
				submitform( pressbutton );
			}
			
		}break;
		default:
		{
			
		}
	}	
	
}


function submitform(pressbutton){
	if (pressbutton) {
		document.adminForm.task.value=pressbutton;
	}
	if (typeof document.adminForm.onsubmit == "function") {
		document.adminForm.onsubmit();
	}
	document.adminForm.submit();
}

function setIndexCombo( obj, value )
{
    var i	=	0;
    
    for (i=0;i< obj.length;i++){
       if (obj.options[i].value == value ){    	   
    	   obj.selectedIndex = i;
    	   break;
       }
          
    }
    
	
}


</script>
</head>
<body>

<div id='content'>

	<div style="text-align:right;">Bem vindo, <%=session.getAttribute( "username" ) %>. <a href="/repositoriovideo/home/">Home</a> <a href="/repositoriovideo/login/?action=logout">Sair</a></div>
	<h1><a href="/repositoriovideo/cadastro/usuario/editar/?action=editar&NID_USUARIO=0&NID_EMPRESA=<%=request.getParameter("NID_EMPRESA") %>&NID_DEPARTAMENTO=<%=request.getParameter("NID_DEPARTAMENTO") %>">Cadastro de Usuário</a></h1>

        
     	<div style="width:100%;margin-left:0px;">
     	
	     	
	    		<fieldset style="width:100%;text-align:center;background-color:#f7f7f7;">
	    			
	    			
	    			<table style="width:100%;">
	    				<tbody>
	    					<tr>
	    					
	    						<td>
	    							
	    							<div onclick="javascript:window.location.href='/repositoriovideo/cadastro/usuario/?NID_USUARIO=<%=NID_USUARIO%>&NID_EMPRESA=<%=NID_EMPRESA%>&NID_DEPARTAMENTO=<%=NID_DEPARTAMENTO%>';" class="cadastro_menu"><h2 class="menu">Fechar</h2></div>
										
									<div onclick="javascript: submitbutton('save');" class="cadastro_menu"><h2 class="menu">Salvar</h2></div>
									
									
	    						</td>
	    					</tr>
	    					
	    				</tbody>
	    			</table>
	             	
	             	
	    		</fieldset>
	    		
	    		
	    		
	    	
	    	<br>
     		
     	
     	</div>
    

	    			
	    <div style="width:1000px;margin-left:0px;">
	    
     		<form action="index.jsp" method="post" name="adminForm">
     		<input type="hidden" name="task" value="">
     		<input type="hidden" name="NID_EMPRESA" value="<%=NID_EMPRESA%>">
     		<input type="hidden" name="CTIPO_ATUAL" value="<%=CTIPO%>">
     		
	     		<table class="cadastro">
	   				<tbody>
	   					<tr class="linha1">   					
	   						<td class="label">NID_USUARIO</td>
	   						<td class="conteudo"><input type="text" name="NID_USUARIO" readonly value="<%=NID_USUARIO%>"></td>
	   					</tr>
	   					<tr class="linha2">   					
	   						<td class="label">VNOME</td>
	   						<td class="conteudo"><input type="text" name="VNOME" value="<%=VNOME%>"></td>
	   					</tr>
	   					<tr class="linha1">   					
	   						<td class="label">NMATRICULA</td>
	   						<td class="conteudo"><input type="text" name="NMATRICULA" value="<%=NMATRICULA%>"></td>
	   					</tr>
	   					<tr class="linha1">   					
	   						<td class="label">VCARGO</td>
	   						<td class="conteudo"><input type="text" name="VCARGO" value="<%=VCARGO%>"></td>
	   					</tr>
	   					<tr class="linha1">   					
	   						<td class="label">VEMAIL</td>
	   						<td class="conteudo"><input type="text" name="VEMAIL" value="<%=VEMAIL%>"></td>
	   					</tr>
	   					<tr class="linha1">   					
	   						<td class="label">VLOGIN</td>
	   						<td class="conteudo"><input type="text" name="VLOGIN" value="<%=VLOGIN%>"></td>
	   					</tr>
	   					<tr class="linha1">   					
	   						<td class="label">VSENHA</td>
	   						<td class="conteudo"><input type="text" name="VSENHA" value="<%=VSENHA%>"></td>
	   					</tr>
	   					<tr class="linha1">   					
	   						<td class="label">CTIPO</td>
	   						<td class="conteudo">
	   						
	   						<select name="CTIPO" id="CTIPO">
	   							<option value="N">Registrado</option>
	   							<option value="E">Editor</option>
	   							<option value="A">Administrador</option>
	   						</select>
	   						</td>
	   					</tr>
	   					<tr class="linha1">   					
	   						<td class="label">NID_DEPARTAMENTO</td>
	   						<td class="conteudo"><input type="text" name="NID_DEPARTAMENTO" value="<%=NID_DEPARTAMENTO%>"></td>
	   					</tr>
	   					
	   					
	   				</tbody>
	   			</table>
	    	</form>		
	    			
  						
  		</div>			
		

		<br>
		<br>
         
       

</div>

<script>
	setIndexCombo( document.getElementById("CTIPO"), '<%=CTIPO%>' );
</script>		


</body>
</html>